package net.towee.client.com.stuff.view;

import net.towee.client.com.stuff.presenter.GroupsFinderPresenter.Display;
import net.towee.client.i18n.ToweeConstants;
import net.towee.client.resources.ClientResource;
import net.towee.client.widgets.DataHelpers;
import net.towee.client.widgets.ExtFlowPanel;
import net.towee.client.widgets.ListItemWidget;
import net.towee.client.widgets.SheduledInputBox;
import net.towee.client.widgets.SpanPanel;
import net.towee.client.widgets.UnorderedListWidget;
import net.towee.model.group.GroupProfile;
import net.towee.model.utils.Pair;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.HasChangeHandlers;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.event.logical.shared.HasOpenHandlers;
import com.google.gwt.event.logical.shared.OpenEvent;
import com.google.gwt.event.logical.shared.OpenHandler;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HasWidgets;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;
import com.google.inject.Inject;

public class GroupsFinderView extends FlowPanel implements Display,
		HasOpenHandlers<Pair<GroupProfile, HasWidgets>> {
	
	private class GroupShowSelector extends UnorderedListWidget implements ClickHandler {
		public ListItemWidget all = new ListItemWidget();
		public ListItemWidget my = new ListItemWidget();
		private Widget active = null;
		
		public GroupShowSelector() {
			setStyleName( resource.css().groupFindeTypeSelector() );
			
			all.setText( constants.allGroups() );
			my.setText( constants.myGroups() );
			
			add(all);
			add(my);
			
			all.addClickHandler(this);
			my.addClickHandler(this);
			
			active = all;
			active.getElement().setAttribute("active", "");
		}

		@Override
		public void onClick(ClickEvent event) {
			if(active != null)
				active.getElement().removeAttribute("active");
			
			active = (Widget) event.getSource();
			active.getElement().setAttribute("active", "");
		}
	}
	
	protected final ToweeConstants constants;
	protected final ClientResource resource;

	protected SheduledInputBox searchInput;
	protected ExtFlowPanel resultBox = new ExtFlowPanel();

	protected final FlowPanel leftColumn = new FlowPanel();
	protected final FlowPanel rightColumn = new FlowPanel();
	protected final FlowPanel groupInfo = new FlowPanel();

	protected final Button moreButton = new Button();
	protected final GroupShowSelector showSelector;

	@Inject
	public GroupsFinderView(ToweeConstants constants, ClientResource resource) {
		this.constants = constants;
		this.resource = resource;
		this.showSelector = new GroupShowSelector();

		moreButton.setText(constants.moreSearchResult());
		moreButton.setStyleName(resource.css().moreSearchResultButton());

		// Create columns and set styles of container
		createColumns();
	}

	/**
	 * Build columns
	 */
	private void createColumns() {
		// Create columns container
		setStyleName(resource.css().absoluteMaximized());
		addStyleName(resource.css().stuffGroupFinder());

		// Create columns
		createLeftColumn();
		createRightColumn();
	}

	/**
	 * Left column
	 */
	protected void createLeftColumn() {
		// Set style and add to columns
		leftColumn.getElement().setAttribute("column", "left");
		add(leftColumn);

		// Create type selector
		leftColumn.add( showSelector );
		
		// Create input box
		searchInput = new SheduledInputBox(constants.enterSearchRequest());
		FlowPanel inputContainer = new FlowPanel();
		inputContainer.add(searchInput);
		inputContainer.setStyleName(resource.css().roundedInputBox());
		leftColumn.add(inputContainer);

		// Create result box
		createResultBox();
	}

	/**
	 * Build box for search result
	 */
	private void createResultBox() {
		// Create result box
		resultBox.getElement().setAttribute("result", "");
		leftColumn.add(resultBox);
	}

	/**
	 * Right column
	 */
	private void createRightColumn() {
		// Set style and add to columns
		rightColumn.getElement().setAttribute("column", "right");
		add(rightColumn);
	}

	@Override
	public void addGroup(final GroupProfile group, boolean isInGroup) {
		// Create item container
		ExtFlowPanel item = new ExtFlowPanel();
		item.setStyleName(resource.css().lobbyGroupItem());

		// Add group name
		FlowPanel container = new FlowPanel();
		FlowPanel nameContainer = new FlowPanel();
		container.add(nameContainer);
		SpanPanel name = new SpanPanel(group.getIdentity().getPrintName());
		SpanPanel alias = new SpanPanel(group.getIdentity().getAlias());
		nameContainer.add(name);
		nameContainer.add(new SpanPanel(" - "));
		nameContainer.add(alias);

		// In group flag
		if (isInGroup) {
			container.getElement().getStyle().setProperty("borderLeft", "8px solid green");
			container.getElement().getStyle().setProperty("padding", "5px 5px 5px 8px");
		} else {
			container.getElement().getStyle().setProperty("padding", "5px 5px 5px 16px");
		}

		// Description
		SpanPanel desc = new SpanPanel(DataHelpers.cutString(group.getDescription(), 90));
		container.add(desc);
		item.add(container);

		name.setStyleName(resource.css().lobbyGroupName());
		alias.setStyleName(resource.css().lobbyGroupAlias());

		item.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				OpenEvent.fire(GroupsFinderView.this, new Pair<GroupProfile, HasWidgets>(group,
						rightColumn));
			}
		});

		moreButton.removeFromParent();
		resultBox.add(item);
		resultBox.add(moreButton);
	}

	@Override
	public HasChangeHandlers getSearchArea() {
		return searchInput;
	}

	@Override
	public HasClickHandlers getMoreResultButton() {
		return moreButton;
	}

	@Override
	public String getSearchString() {
		return searchInput.getText().trim();
	}

	@Override
	public void showEmptyResult() {
		Label noResult = new Label(constants.noGroupsFound());
		noResult.getElement().removeAttribute("class");
		noResult.getElement().setAttribute("noresult", "");
		resultBox.add(noResult);
	}

	@Override
	public void onStartSearching() {
		resultBox.getElement().getStyle().setOpacity(0.2);
	}

	@Override
	public void onEndSearching() {
		resultBox.getElement().getStyle().setOpacity(1);
	}

	@Override
	public void clearGroups() {
		resultBox.clear();
		moreButton.setVisible(true);
	}

	@Override
	public HasOpenHandlers<Pair<GroupProfile, HasWidgets>> getGroupOpenHandlers() {
		return this;
	}

	@Override
	public HandlerRegistration addOpenHandler(OpenHandler<Pair<GroupProfile, HasWidgets>> handler) {
		return addHandler(handler, OpenEvent.getType());
	}

	@Override
	public void noMore() {
		moreButton.setVisible(false);
	}

	@Override
	public HasClickHandlers getAllGroupsButton() {
		return showSelector.all;
	}
	
	@Override
	public HasClickHandlers getMyGroupsButton() {
		return showSelector.my;
	}
}